package com.cloudkinto.controller.order.outbound;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.outbound.BackOutboundShelfBo;
import com.cloudkinto.bo.order.outbound.OutboundCancelQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.enumType.order.outbound.OutboundOrderKind;
import com.cloudkinto.service.order.outbound.OutboundCancelService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.outbound.BackOutboundShelfVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 出库单截单 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-12-09
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "wms出库单截单")
@RequestMapping("/api/admin/wms/cancel")
public class OutboundCancelWmsController extends BaseController {
    private final OutboundCancelService cancelService;

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "出库单截单查询初始化", notes = "出库单截单查询初始化")
    public SingleResult pageInit() {
        return cancelService.pageInit(getCurrentUserId(), getTenantId());
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "出库单截单查询（分页）", notes = "出库单截单查询（分页）")
    public PageResult listPage(@RequestBody @Validated OutboundCancelQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return cancelService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusSta")
    @ApiOperation(value = "出库单截单状态统计", notes = "出库单截单状态统计")
    public SingleResult listPageStatusSta(@RequestBody @Validated OutboundCancelQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return cancelService.listPageStatusSta(bo);
    }

    @UserSessionRequired
    @GetMapping("/getBackOutboundDetail/{id}")
    @ApiModelProperty(value = "获取回库上架详情", notes = "获取回库上架详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = BackOutboundShelfVo.class))
    public SingleResult getBackOutboundDetail(@PathVariable("id") Long id) {
        return SingleResult.success(cancelService.getBackOutboundDetail(id));
    }

    @UserSessionRequired
    @PostMapping("backOutboundShelf")
    @ApiOperation(value = "回库上架", notes = "回库上架")
    public SingleResult backOutboundShelf(@RequestBody @Validated BackOutboundShelfBo req) {
        return cancelService.backOutboundShelf(req, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/markFail/{id}")
    @ApiOperation(value = "标记失败", notes = "标记失败(id为截单id)")
    public SingleResult markFail(@PathVariable("id") Long id) {
        return cancelService.markFail(id, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PostMapping("/backOutboundShelf/batch")
    @ApiOperation(value = "回库上架-批量", notes = "回库上架-批量")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CommonBatchOperationVo.class))
    public SingleResult batchBackOutboundShelf(Long[] ids, String type) {
        OutboundOrderKind outboundOrderKind = OutboundOrderKind.fromValue(type);
        if (outboundOrderKind == null) {
            return SingleResult.failure("type错误");
        }
        CommonBatchOperationVo operationVo = null;
        if (outboundOrderKind.getValue().equals("allocate")) {
            operationVo = cancelService.batchBackAllocateShelf(Arrays.asList(ids), getCurrentUserId(), getUserName());
        } else if (outboundOrderKind.getValue().equals("outbound")) {
            operationVo = cancelService.batchBackOutboundShelf(Arrays.asList(ids), getCurrentUserId(), getUserName());
        }
        return SingleResult.success(operationVo);
    }

    @UserSessionRequired
    @PutMapping("/markFail/batch")
    @ApiOperation(value = "标记失败-批量", notes = "标记失败-批量")
    public SingleResult markFail(Long[] ids, String type) {
        OutboundOrderKind outboundOrderKind = OutboundOrderKind.fromValue(type);
        if (outboundOrderKind == null) {
            return SingleResult.failure("type错误");
        }
        return SingleResult.success(cancelService.batchMarkFail(Arrays.asList(ids), outboundOrderKind, getCurrentUserId(), getUserName()));
    }
}

